package com.complex.client.ui;

import com.complex.client.service.DAOWebClient;
import com.complex.client.service.gen.*;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeModel;
import javax.xml.namespace.QName;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: AGrankin
 * Date: 23.08.13
 * Time: 19:46
 * To change this template use File | Settings | File Templates.
 */
public class Controller {

    private MainFrame mainFrame;

    private static DAOWebService daoWebService;
    private static List<WebServiceTreeNode> incomingNodes;
    private static TreeNodesModel treeNodesModel = null;
    private static TableAttributesModel tableAttributesModel = null;

    static {
        try {
            daoWebService = DAOWebClient.getInstance(new URL("http://agrankin:7001/ComplexDAOWebService/DAOWebServerService?WSDL")).getDAOWebService();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            incomingNodes = daoWebService.getNodesMethod().getTNodeList();
        } catch (GetDAOBeanException_Exception e) {
            e.printStackTrace();
        }

        treeNodesModel = new TreeNodesModel();
        treeNodesModel.setData(incomingNodes);
        tableAttributesModel = new TableAttributesModel(null);
        tableAttributesModel.setData(null);
        MainFrame.setModels(treeNodesModel, tableAttributesModel);

    }

    public Controller(MainFrame mainFrame) {

        this.mainFrame = mainFrame;


        this.mainFrame.addNodeSelectionListener(new TreeSelectionListener() {    //------------------- тут что-то не так Exception при добавлении ноды
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                WebServiceTreeNode node = Controller.this.mainFrame.getSelected();

                tableAttributesModel.setData(node.getNodeAttributes());
                Controller.this.mainFrame.setTableModel(tableAttributesModel);
//                Controller.this.mainFrame.setTableModel(new TableAttributesModel(node.getNodeAttributes()));
                System.out.println(e);
            }
        });

        this.mainFrame.setAddNodeButtonListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nodeName;
                if (Controller.this.mainFrame.getNodeNameFieldText().equals("")) {
                    nodeName = "default";
                    System.out.println("node name: " + nodeName);
                } else {
                    nodeName = Controller.this.mainFrame.getNodeNameFieldText();
                    System.out.println("node name: " + nodeName);
                }
                if (Controller.this.mainFrame.getAddNodeRadioState()) {
                    daoWebService.insertNodeMethod(1, nodeName);
                    System.out.println("root Node created");
                } else if (Controller.this.mainFrame.getAddChildRadioState()){
                    daoWebService.insertNodeMethod(Controller.this.mainFrame.getSelected().getNodeID(), nodeName);
                    System.out.println("child Node created");
                }

                try {
                    incomingNodes = daoWebService.getNodesMethod().getTNodeList();
                } catch (GetDAOBeanException_Exception e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }

                treeNodesModel.setData(incomingNodes);
                Controller.this.mainFrame.setTreeModel(treeNodesModel);

            }
        });

        this.mainFrame.setRemoveNodeButtonListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        this.mainFrame.setAddAttributeButtonListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        this.mainFrame.setRemoveAttributeButtonListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

    }
}
